import { delNotice, getNotice, listNotice } from '@/api/system/notice'

import { formatDate } from '@/utils/common'
import { useDicts } from '@/utils/dict'
import { message } from '@/utils/message'
import type { PaginationProps } from '@pureadmin/table'
import { ElMessageBox } from 'element-plus'
import { onMounted, reactive, ref, toRefs } from 'vue'

export function useNotice() {
  const data = reactive({
    form: {
      noticeId: undefined,
      noticeTitle: undefined,
      noticeType: undefined,
      noticeContent: undefined,
      status: '0',
    },
    queryParams: {
      noticeTitle: undefined,
      createBy: undefined,
      noticeType: undefined,
    },
  })
  const { form, queryParams } = toRefs(data)
  const noticeId = ref(0)
  const title = ref('')
  const ids = ref([])
  const single = ref(true)
  const multiple = ref(true)
  const dataList = ref([])
  const loading = ref(true)
  const showSelection = true
  const formDialogVisible = ref(false)
  const { sys_notice_status, sys_notice_type } = useDicts('sys_notice_status', 'sys_notice_type')
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
  })
  const columns: TableColumnList = [
    {
      type: 'selection',
      width: 55,
      align: 'left',
      hide: ({ checkList }) => !checkList.includes('勾选列'),
    },
    {
      label: '序号',
      type: 'index',
      width: 70,
      hide: ({ checkList }) => !checkList.includes('序号列'),
    },
    {
      label: '公告编码',
      prop: 'noticeId',
      minWidth: 100,
    },
    {
      label: '公告标题',
      prop: 'noticeTitle',
      minWidth: 150,
    },
    {
      label: '公告类型',
      prop: 'noticeType',
      minWidth: 130,
      cellRenderer: (scope) => {
        return <DictTag options={sys_notice_type} value={scope.row.noticeType} />
      },
    },
    {
      label: '状态',
      prop: 'status',
      minWidth: 130,
      cellRenderer: (scope) => {
        return <DictTag options={sys_notice_status} value={scope.row.status} />
      },
    },
    {
      label: '创建者',
      prop: 'createBy',
      minWidth: 120,
    },
    {
      label: '创建时间',
      minWidth: 180,
      prop: 'createTime',
      formatter: ({ createTime }) => formatDate(createTime),
    },
    {
      label: '操作',
      fixed: 'right',
      width: 180,
      slot: 'operation',
    },
  ]

  /** 重置新增的表单  */
  function reset() {
    form.value = {
      noticeId: undefined,
      noticeTitle: undefined,
      noticeType: undefined,
      noticeContent: undefined,
      status: '0',
    }
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset()
    title.value = '添加通知公告'
    formDialogVisible.value = true
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset()
    noticeId.value = row.noticeId
    getNotice(noticeId.value).then((response: any) => {
      console.log(response)
      form.value = response.data
      formDialogVisible.value = true
      title.value = '修改通知公告'
    })
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const noticeIds = row.noticeId || ids.value
    ElMessageBox.confirm('数据一经删除不可恢复，请再次确认！', '提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(async () => {
      await delNotice(noticeIds)
      message('删除成功', { type: 'success' })
      onSearch()
    })
  }

  /** 改变分页大小 */
  function handleSizeChange(val: number) {
    pagination.pageSize = val
    onSearch()
  }

  /** 改变当前分页 */
  function handleCurrentChange(val: number) {
    pagination.currentPage = val
    onSearch()
  }

  /** 改变多选项 */
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.noticeId)
    single.value = selection.length != 1
    multiple.value = !selection.length
  }

  /** 搜索按钮操作 */
  async function onSearch() {
    loading.value = true
    const params = {
      ...queryParams.value,
      pageNum: pagination.currentPage,
      pageSize: pagination.pageSize,
    }
    const { rows, total } = await listNotice(params)
    dataList.value = rows
    pagination.total = total
    loading.value = false
  }

  /** 重置按钮操作 */
  const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
    onSearch()
  }

  onMounted(() => {
    onSearch()
  })

  return {
    form,
    title,
    noticeId,
    loading,
    columns,
    multiple,
    dataList,
    pagination,
    queryParams,
    showSelection,
    formDialogVisible,
    sys_notice_type,
    onSearch,
    resetForm,
    handleAdd,
    handleUpdate,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
  }
}
